/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.info.controller  
 */
package com.apache.info.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.apache.action.PlatFromAction;
import com.apache.api.manager.BaseManager;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultMsg;
import com.apache.api.vo.Token;
import com.apache.cache.util.Validator;
import com.apache.database.constant.SystemTools;
import com.apache.database.model.Page;
import com.apache.info.entity.PubMetadata;
import com.apache.info.entity.PubSortInfo;
import com.apache.info.manager.InfoReleaseManager;
import com.apache.info.manager.PubMetadataManager;
import com.apache.info.manager.PubSortInfoManager;
import com.apache.info.template.BuildProjectFactory;
import com.apache.tools.StrUtil;

/**
 * description:  分类Action
 * @author Hou Dayu 创建时间：2015-8-27  
 * @Copyright(c)2014:北京--软件技术有限公司
 */
@Controller
@RequestMapping("info/core/")
public class PubSortInfoAction extends PlatFromAction<PubSortInfo> {

	@Autowired
	private PubSortInfoManager pubSortInfoManager;
	@Autowired
	private PubMetadataManager pubMetadataManager;
	@Autowired
	private InfoReleaseManager InfoReleaseManager;

	@RequestMapping("info!data.action")
	@ResponseBody
	protected Object data(HttpServletRequest request, String page, String rows) {
		ParamsVo<PubSortInfo> vo = new ParamsVo<PubSortInfo>();
		PubSortInfo sort = new PubSortInfo();
		if (Validator.isNotNull(request.getParameter("sortCode"))) {
			sort.setSortCode(request.getParameter("sortCode"));
		}
		if (Validator.isNotNull(request.getParameter("sortName"))) {
			sort.setSortName(request.getParameter("sortName"));
		}
		if (Validator.isNotNull(request.getParameter("sortStatus"))) {
			sort.setSortStatus(request.getParameter("sortStatus"));
		} else {
			sort.setSortStatus("");
		}
		if (Validator.isNotNull(request.getParameter("sortDisable"))) {
			sort.setSortDisable(request.getParameter("sortDisable"));
		} else {
			sort.setSortDisable("");
		}
		vo.setParams("pageIndex", page);
		vo.setParams("pageSize", rows);
		vo.setObj(sort);
		Page pageList = pubSortInfoManager.getPageInfo(vo);
		Map<String, Object> mapData = new HashMap<String, Object>();
		mapData.put("total", pageList.getCount());
		mapData.put("rows", pageList.getPageObjects());
		return mapData;
	}

	@Token(save = true)
	@RequestMapping("add.action")
	protected ModelAndView add(HttpServletRequest request) {
		ModelAndView view = new ModelAndView(ADD);
		String sortId = request.getParameter("sortId");
		PubSortInfo sort = new PubSortInfo();
		if (Validator.isNotNull(sortId)) {
			ParamsVo<PubSortInfo> vo = new ParamsVo<PubSortInfo>();
			vo.setInfoId(sortId);
			sort = (PubSortInfo) pubSortInfoManager.getInfoById(vo);
		}
		view.addObject("item", sort);
		return view;
	}

	@Token(remove = true)
	@RequestMapping("save.action")
	@ResponseBody
	public ResultMsg save(HttpServletRequest request, HttpServletResponse response, @ModelAttribute PubSortInfo item) {
		ResultMsg rmsg = new ResultMsg("T", "操作成功！");
		ParamsVo<PubSortInfo> vo = new ParamsVo<PubSortInfo>();
		if (Validator.isNull(item.getSortId())) {
			vo.setInfoId(item.getSortCode());
			Object obj = pubSortInfoManager.getInfoById(vo);
			if (!Validator.isEmpty(obj)) {
				rmsg = getResultMsg(request, "分类[" + item.getSortCode() + "]已存在,请选择其他的名字");
				return rmsg;
			}
			vo.setParams("mainTab", request.getParameter("mainTab"));
			if (Validator.isNull(item.getInfoTableName())) {
				//设置表单前缀 INFO_TABLE_NAME配置
				String tableNameQian = "Pub_";
				try {
					String qianz = SystemTools.getInstance().getValue("INFO_TABLE_NAME");
					if (StrUtil.isNotNull(qianz)) {
						if (qianz.indexOf("_") < 0) {
							qianz += "_";
						}
						tableNameQian = qianz;
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				String tableName = tableNameQian + item.getSortCode();
				item.setInfoTableName(tableName.toUpperCase());
			}
			item.setSortStatus("0");
			vo.setObj(item);
			String id = pubSortInfoManager.saveInfo(vo);
			if (Validator.isNull(id)) {
				rmsg = getResultMsg(request, "分类信息添加失败！");
			}
		} else {
			vo.setObj(item);
			boolean mark = pubSortInfoManager.editInfo(vo);
			if (!mark) {
				rmsg = getResultMsg(request, "分类信息修改失败！");
			}
		}
		return rmsg;
	}

	@RequestMapping("del.action")
	@ResponseBody
	public ResultMsg delete(HttpServletRequest request, String id) {
		ResultMsg rmsg = new ResultMsg("T", "删除成功！");
		ParamsVo<PubSortInfo> vo = new ParamsVo<PubSortInfo>();
		if (isNotNull(id)) {
			vo.setInfoId(id);
			PubSortInfo sort = (PubSortInfo) pubSortInfoManager.getInfoById(vo);
			if (Validator.isEmpty(sort)) {
				rmsg = getResultMsg(request, "删除的信息不存在！");
				return rmsg;
			}
			if ("1".equals(sort.getSortStatus())) {
				rmsg = getResultMsg(request, "信息已被启用,不能做删除操作！");
				return rmsg;
			} else {
				ParamsVo<PubMetadata> met = new ParamsVo<PubMetadata>();
				PubMetadata pm = new PubMetadata();
				pm.setSortId(sort.getSortId());
				met.setObj(pm);
				long count = pubMetadataManager.countInfo(met);
				if (count > 0) {
					rmsg = getResultMsg(request, "信息存在相应元素,请先删除其下元素！");
					return rmsg;
				}
			}
			boolean mark = getBaseManager().deleteInfo(vo);
			if (!mark) {
				rmsg.setFlag("F");
				rmsg.setMsg("删除失败！");
			}
		} else {
			rmsg.setFlag("H");
			rmsg.setMsg("请不要恶意操作！");
		}
		return rmsg;
	}

	@RequestMapping("publish.action")
	@ResponseBody
	public ResultMsg publish(HttpServletRequest request, String id) {
		ResultMsg rmsg = new ResultMsg("T", "启用成功！");
		ParamsVo vo = new ParamsVo();
		if (isNotNull(id)) {
			vo.setInfoId(id);
			PubSortInfo sort = (PubSortInfo) pubSortInfoManager.getInfoById(vo);
			if (Validator.isEmpty(sort)) {
				rmsg = getResultMsg(request, "分类信息不存在！");
				return rmsg;
			}
			ParamsVo<PubMetadata> met = new ParamsVo<PubMetadata>();
			PubMetadata pm = new PubMetadata();
			pm.setSortId(sort.getSortId());
			met.setObj(pm);
			long count = pubMetadataManager.countInfo(met);
			if (count <= 0) {
				rmsg = getResultMsg(request, "分类下不存在相应元素,不能启用此分类！");
				return rmsg;
			}
			vo.setParams("tableName", sort.getInfoTableName());
			vo.setKey("exisTable");
			count = Long.valueOf(String.valueOf(InfoReleaseManager.execute(vo)));
			if (count > 0) {
				rmsg = getResultMsg(request, "数据表已存在不能再次启用！");
				return rmsg;
			} else {
				vo.setKey("createTable");
				vo.setParams("sort", sort);
				InfoReleaseManager.execute(vo);
			}
		} else {
			rmsg.setFlag("H");
			rmsg.setMsg("请不要恶意操作！");
		}
		return rmsg;
	}

	@RequestMapping("stop.action")
	@ResponseBody
	public ResultMsg stop(HttpServletRequest request, String id) {
		ResultMsg rmsg = new ResultMsg("T", "关闭成功！");
		ParamsVo vo = new ParamsVo();
		if (isNotNull(id)) {
			vo.setInfoId(id);
			PubSortInfo sort = (PubSortInfo) pubSortInfoManager.getInfoById(vo);
			if (Validator.isEmpty(sort)) {
				rmsg = getResultMsg(request, "分类信息不存在！");
				return rmsg;
			}
			vo.setKey("dropTable");
			vo.setParams("sort", sort);
			InfoReleaseManager.execute(vo);
		} else {
			rmsg.setFlag("H");
			rmsg.setMsg("请不要恶意操作！");
		}
		return rmsg;
	}

	@RequestMapping("createCode.action")
	public ModelAndView createCode(HttpServletRequest request) {
		ModelAndView view = new ModelAndView("info/create-code");
		return view;
	}

	@ResponseBody
	@RequestMapping("buildProject")
	public ResultMsg buildProject(HttpServletRequest request) {
		String ids = request.getParameter("tempIds");
		String filePath = request.getParameter("filePath");
		String projectName = request.getParameter("projectName");
		String packPath = request.getParameter("packPath");
		String createTemplate = request.getParameter("createTemplate");
		Map<String, String> param = new HashMap<String, String>();
		param.put("filePath", filePath);
		param.put("ids", ids);
		param.put("projectName", projectName);
		param.put("packPath", packPath);
		param.put("createTemplate", createTemplate);
		boolean mark = BuildProjectFactory.instance().ctreateProject(param);
		ResultMsg rmsg = new ResultMsg("F", "工程生成失败！");
		if (mark) {
			rmsg = new ResultMsg("F", "工程生成成功！");
		}
		return rmsg;
	}

	protected BaseManager<PubSortInfo> getBaseManager() {
		return pubSortInfoManager;
	}

	protected String getPrefix() {
		return "info/sort-info-";
	}

	private ResultMsg getResultMsg(HttpServletRequest request, String msg) {
		request.getSession().setAttribute("formToken", request.getAttribute("formToken"));
		ResultMsg rmsg = new ResultMsg("F", msg);
		return rmsg;
	}
}
